package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import java.beans.PropertyChangeEvent;
import org.testis.gui.mvc.controller.testdesign.DataObjectController;
import org.testis.gui.mvc.controller.DynamicMapControllerList;
import org.testis.gui.mvc.controller.dynamicmap.DynamicMapTreeController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.testdesign.ConditionsController;
import org.testis.gui.mvc.utilities.ParameterTableModel;
import org.testis.gui.mvc.utilities.Validatable;
import org.testis.gui.mvc.utilities.ValidationUtils;
import org.testis.gui.mvc.view.labels.LabelPairSettingsView;
import java.util.LinkedList;
import org.orm.PersistentException;

/**
 *
 * @author  Cahit Emir Erel
 */
public class DataObjectView extends AtofBaseDialog implements
        AbstractViewPanel,
        Validatable {

    private static final long serialVersionUID = 91632L;
    private boolean m_editMode;
    private String m_windowTitle;
    private boolean mousePressed = false;
    private Point m_start = null;
    private int m_startingRow = -1;
    private String m_currentParameter;
    private String m_currentValue;
    private boolean m_removeViewFromDynamicMapTreeController = false;
    private LabelPairSettingsView m_labelSettingsView;

    public DataObjectView(Frame parent,
            boolean modal,
            boolean editMode,
            ParameterTableModel tableModel) {
        super(parent,
                modal);
        m_editMode = editMode;
        if (editMode) {
            m_windowTitle = "Edit Data Object";
        } else {
            m_windowTitle = "Add Data Object";
        }
        m_labelSettingsView = new LabelPairSettingsView();
        initComponents();
        parameterTable.setModel(tableModel);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveButton = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        parameterScrollPane = new javax.swing.JScrollPane();
        parameterTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        modulTypeLabel = new javax.swing.JLabel();
        modulTypeTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        valueLabel = new javax.swing.JLabel();
        expectedResultChoice = new java.awt.Choice();
        nameTextField = new javax.swing.JTextField();
        btnLanguageVariable = new javax.swing.JButton();
        valueTextArea = new java.awt.TextArea();
        keyLabel = new javax.swing.JLabel();
        keyChoice = new javax.swing.JComboBox();
        btnProfileVariable = new javax.swing.JButton();
        expectedResultLabel = new javax.swing.JLabel();
        addKeyButton = new javax.swing.JButton();
        removeKeyButton = new javax.swing.JButton();
        labelSettingsScrollPane = new javax.swing.JScrollPane();
        labelSettingsScrollPane.setViewportView(m_labelSettingsView);
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(m_windowTitle);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        String label;
        if (m_editMode) {
            label = "Save Changes";
        } else {
            label = "Add Data Object";
        }
        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        parameterTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        parameterTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        parameterTable.getTableHeader().setReorderingAllowed(false);
        parameterTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                parameterTableMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                parameterTableMouseReleased(evt);
            }
        });
        parameterTable.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                parameterTableMouseDragged(evt);
            }
        });
        parameterScrollPane.setViewportView(parameterTable);

        modulTypeLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        modulTypeLabel.setText("Modul Type :");

        modulTypeTextField.setEditable(false);
        modulTypeTextField.setFont(new java.awt.Font("Dialog", 0, 11));

        nameLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        nameLabel.setText("Name :");

        valueLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        valueLabel.setText("Value :");

        String[] choices = TestDesignControllerList.newInstance().getDataObjectController().operationGetExpectedResultTypesList();
        for (int i=0;i<choices.length;i++) {
            expectedResultChoice.add(choices[i]);
        }
        choices=null;
        expectedResultChoice.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                expectedResultChoiceItemStateChanged(evt);
            }
        });

        nameTextField.setFont(new java.awt.Font("Dialog", 0, 11));
        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });
        nameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                nameTextFieldFocusLost(evt);
            }
        });

        btnLanguageVariable.setText("Language Variable");
        btnLanguageVariable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLanguageVariableActionPerformed(evt);
            }
        });

        valueTextArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                valueTextAreaFocusLost(evt);
            }
        });

        keyLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        keyLabel.setText("Key :");

        keyChoice.setModel(new javax.swing.DefaultComboBoxModel(new Object[0]));
        keyChoice.setLightWeightPopupEnabled(false);
        keyChoice.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                keyChoiceItemStateChanged(evt);
            }
        });
        keyChoice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyChoiceActionPerformed(evt);
            }
        });

        btnProfileVariable.setText("Profile Variable");
        btnProfileVariable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProfileVariableActionPerformed(evt);
            }
        });

        expectedResultLabel.setFont(new java.awt.Font("Dialog", 0, 11));
        expectedResultLabel.setText("Expected Result * :");

        addKeyButton.setText("Add Key");
        addKeyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addKeyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(valueLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(keyLabel, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(nameLabel)
                            .addComponent(modulTypeLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(valueTextArea, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(keyChoice, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(nameTextField, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(modulTypeTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(addKeyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(expectedResultLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(expectedResultChoice, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnProfileVariable)
                    .addComponent(btnLanguageVariable))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {expectedResultLabel, keyLabel, modulTypeLabel, nameLabel, valueLabel});

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnLanguageVariable, btnProfileVariable});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(modulTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modulTypeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(keyChoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(keyLabel)
                    .addComponent(addKeyButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnProfileVariable)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLanguageVariable))
                    .addComponent(valueLabel)
                    .addComponent(valueTextArea, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(expectedResultLabel)
                    .addComponent(expectedResultChoice, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        removeKeyButton.setText("Remove Key");
        removeKeyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeKeyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(parameterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 724, Short.MAX_VALUE)
                    .addComponent(removeKeyButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(parameterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeKeyButton)
                .addContainerGap())
        );

        jTabbedPane1.addTab("General", jPanel1);
        jTabbedPane1.addTab("Labels", labelSettingsScrollPane);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(closeButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {closeButton, saveButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeButton)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        ConditionsController.createInstance(m_editMode).initialize();
        jTabbedPane1.addTab("Conditions", ConditionsController.getInstance().getConditionView());

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addKeyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addKeyButtonActionPerformed

        TestDesignControllerList.newInstance().getDataObjectController().
                operationAddKey(keyChoice.isEditable());
}//GEN-LAST:event_addKeyButtonActionPerformed

    private void removeKeyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeKeyButtonActionPerformed

        TestDesignControllerList.newInstance().getDataObjectController().
                operationRemoveKey(parameterTable.getSelectedRow(),
                keyChoice.isEditable());
}//GEN-LAST:event_removeKeyButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (!TestDesignControllerList.newInstance().getDataObjectController().
                checkConstraints()) {
            return;
        }
        if (!validateForm()) {
            return;
        }
        saveButton.setEnabled(false);

        LinkedList<String> labels = m_labelSettingsView.getSelectedLabelNames();
        if (m_editMode) {
            TestDesignControllerList.newInstance().getDataObjectController().
                    operationSaveDataObject(nameTextField.getText(),
                    (ParameterTableModel) parameterTable.getModel(),
                    expectedResultChoice.getSelectedItem(),
                    labels);
        } else {
            try {
                TestDesignControllerList.newInstance().getDataObjectController().
                        operationAddDataObject(nameTextField.getText(),
                        (ParameterTableModel) parameterTable.getModel(),
                        modulTypeTextField.getText(),
                        expectedResultChoice.getSelectedItem(),
                        labels);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        }
        dispose();
}//GEN-LAST:event_saveButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        WindowListener[] listeners = getWindowListeners();
        for (int i = 0; i
                < listeners.length; i++) {
            removeWindowListener(listeners[i]);
        }
        if (m_removeViewFromDynamicMapTreeController) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    removeView(this);
        }
        setEnabled(false);
        TestDesignControllerList.newInstance().getTreeController().
                operationShowTab(0);
        TestDesignControllerList.newInstance().getDataObjectController().
                operationCloseWindow();
        ConditionsController.createInstance(m_editMode).operationClose();
    }//GEN-LAST:event_formWindowClosed

    private void parameterTableMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_parameterTableMouseDragged

        if (mousePressed == true) {
            if (m_start != null && m_start.getY() - evt.getY()
                    > parameterTable.getRowHeight() && m_startingRow != -1
                    && m_startingRow > 0) {
                String targetParameter = (String) parameterTable.getModel().
                        getValueAt(m_startingRow - 1,
                        0);
                String targetValue = (String) parameterTable.getModel().
                        getValueAt(m_startingRow - 1,
                        1);
                parameterTable.getModel().setValueAt(m_currentParameter,
                        m_startingRow - 1,
                        0);
                parameterTable.getModel().setValueAt(m_currentValue,
                        m_startingRow - 1,
                        1);
                parameterTable.getModel().setValueAt(targetParameter,
                        m_startingRow,
                        0);
                parameterTable.getModel().setValueAt(targetValue,
                        m_startingRow,
                        1);
                parameterTable.repaint();
                m_start =
                        evt.getPoint();
                m_startingRow--;
            } else if (m_start != null && m_start.getY() - evt.getY()
                    < -parameterTable.getRowHeight() && m_startingRow != -1 && m_startingRow < parameterTable.getModel().
                    getRowCount() - 1) {
                String targetParameter = (String) parameterTable.getModel().
                        getValueAt(m_startingRow + 1,
                        0);
                String targetValue = (String) parameterTable.getModel().
                        getValueAt(m_startingRow + 1,
                        1);
                parameterTable.getModel().setValueAt(m_currentParameter,
                        m_startingRow + 1,
                        0);
                parameterTable.getModel().setValueAt(m_currentValue,
                        m_startingRow + 1,
                        1);
                parameterTable.getModel().setValueAt(targetParameter,
                        m_startingRow,
                        0);
                parameterTable.getModel().setValueAt(targetValue,
                        m_startingRow,
                        1);
                parameterTable.repaint();
                m_start =
                        evt.getPoint();
                m_startingRow++;
            }
        }
    }//GEN-LAST:event_parameterTableMouseDragged

    private void parameterTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_parameterTableMouseReleased

        mousePressed = false;
        m_start =
                null;
        m_startingRow =
                -1;
    }//GEN-LAST:event_parameterTableMouseReleased

    private void parameterTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_parameterTableMousePressed

        m_start = evt.getPoint();
        mousePressed =
                true;
        parameterTable.changeSelection(parameterTable.rowAtPoint(m_start),
                0,
                false,
                false);
        m_startingRow =
                parameterTable.getSelectedRow();
        m_currentParameter =
                (String) parameterTable.getModel().getValueAt(
                m_startingRow,
                0);
        m_currentValue =
                (String) parameterTable.getModel().getValueAt(m_startingRow,
                1);
        TestDesignControllerList.newInstance().getDataObjectController().
                operationSelectKey(m_currentParameter,
                m_currentValue);
    }//GEN-LAST:event_parameterTableMousePressed

private void btnLanguageVariableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLanguageVariableActionPerformed
    TestDesignControllerList.newInstance().getLanguageVariablesController().
            startLoader();
}//GEN-LAST:event_btnLanguageVariableActionPerformed

private void btnProfileVariableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProfileVariableActionPerformed
    TestDesignControllerList.newInstance().getProfileSettingDetailsController().
            useForDataObject();
}//GEN-LAST:event_btnProfileVariableActionPerformed

private void expectedResultChoiceItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_expectedResultChoiceItemStateChanged
    TestDesignControllerList.newInstance().getDataObjectController().
            operationChangeExpectedResult(expectedResultChoice.getSelectedItem());
}//GEN-LAST:event_expectedResultChoiceItemStateChanged

private void valueTextAreaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_valueTextAreaFocusLost

    TestDesignControllerList.newInstance().getDataObjectController().
            operationChangeValue(valueTextArea.getText());
}//GEN-LAST:event_valueTextAreaFocusLost

private void keyChoiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyChoiceActionPerformed
    TestDesignControllerList.newInstance().getDataObjectController().
            operationChangeKey((String) keyChoice.getSelectedItem(),
            keyChoice.isEditable());
}//GEN-LAST:event_keyChoiceActionPerformed

private void keyChoiceItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_keyChoiceItemStateChanged
}//GEN-LAST:event_keyChoiceItemStateChanged

private void nameTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFieldFocusLost
    TestDesignControllerList.newInstance().getDataObjectController().
            operationChangeName(nameTextField.getText());
}//GEN-LAST:event_nameTextFieldFocusLost

private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
    TestDesignControllerList.newInstance().getDataObjectController().
            operationChangeName(nameTextField.getText());
}//GEN-LAST:event_nameTextFieldActionPerformed

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
    dispose();
}//GEN-LAST:event_closeButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addKeyButton;
    private javax.swing.JButton btnLanguageVariable;
    private javax.swing.JButton btnProfileVariable;
    private javax.swing.JButton closeButton;
    private java.awt.Choice expectedResultChoice;
    private javax.swing.JLabel expectedResultLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JComboBox keyChoice;
    private javax.swing.JLabel keyLabel;
    private javax.swing.JScrollPane labelSettingsScrollPane;
    private javax.swing.JLabel modulTypeLabel;
    private javax.swing.JTextField modulTypeTextField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JScrollPane parameterScrollPane;
    private javax.swing.JTable parameterTable;
    private javax.swing.JButton removeKeyButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel valueLabel;
    private java.awt.TextArea valueTextArea;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                DataObjectController.SHOW_ADD_DATA_OBJECT)) {
            this.centerParent();
        } else if (evt.getPropertyName().equals(
                DataObjectController.CHANGE_MODUL_TYPE)) {
            String type = (String) evt.getNewValue();
            keyChoice.removeAll();
            if (!type.equals("")) {
                String[] choices = TestDesignControllerList.newInstance().
                        getDataObjectController().operationGetKeys(type,
                        m_editMode);
                TestDesignControllerList.newInstance().getDataObjectController().
                        operationRemoveAllKeys(keyChoice.isEditable());
                if (choices != null) {
                    for (int i = 0; i
                            < choices.length; i++) {
                        keyChoice.addItem(choices[i]);
                        TestDesignControllerList.newInstance().
                                getDataObjectController().operationAddKey(
                                choices[i]);
                    }
                    choices = null;
                } else {
                    DynamicMapControllerList.newInstance().
                            getDynamicMapTreeController().addView(this);
                    m_removeViewFromDynamicMapTreeController =
                            true;
                }
            }
            modulTypeTextField.setText(type);
            modulTypeTextField.repaint();
            keyChoice.updateUI();
        } else if (evt.getPropertyName().equals(
                DataObjectController.CHANGE_NAME)) {
            nameTextField.setText((String) evt.getNewValue());
            nameTextField.repaint();
        } else if (evt.getPropertyName().equals(
                DataObjectController.EDITABLE_KEY)) {
            keyChoice.setEditable((Boolean) evt.getNewValue());
            keyChoice.updateUI();
        } else if (evt.getPropertyName().equals(DataObjectController.ADD_KEY)) {
            parameterTable.setModel((ParameterTableModel) evt.getNewValue());
            parameterTable.updateUI();
        } else if (evt.getPropertyName().equals(
                DataObjectController.REMOVE_SELECTED_KEY)) {
            parameterTable.setModel((ParameterTableModel) evt.getNewValue());
            parameterTable.updateUI();
        } else if (evt.getPropertyName().equals(
                DataObjectController.REMOVE_ALL_KEYS)) {
            parameterTable.setModel((ParameterTableModel) evt.getNewValue());
            parameterTable.updateUI();
        } else if (evt.getPropertyName().equals(
                DataObjectController.CHANGE_EXPECTED_RESULT)) {
            expectedResultChoice.select((String) evt.getNewValue());
        } else if (evt.getPropertyName().equals(
                DataObjectController.CHANGE_KEY)) {
            String key = (String) evt.getNewValue();
            if (keyChoice.isEditable()) {
                keyChoice.removeAllItems();
                keyChoice.addItem(key);
            }
            keyChoice.setSelectedItem(key);
            keyChoice.repaint();
        } else if (evt.getPropertyName().equals(
                DataObjectController.CHANGE_VALUE)) {
            valueTextArea.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                DataObjectController.CLEAR_TABLE_SELECTION)) {
            parameterTable.clearSelection();
            parameterTable.repaint();
        } else if (evt.getPropertyName().equals(
                DataObjectController.CHANGE_LAST_WARNING)) {
            String warnMsg = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(null,
                    warnMsg,
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.SHOW_PROPERTIES)) {
            String result = evt.getNewValue().toString();
            int i = result.indexOf("AbstractId");
            if (i != -1) {
                int j = result.indexOf("\n",
                        i);
                if (j != -1) {
                    keyChoice.setSelectedItem(result.substring(i + 11,
                            j));
                } else {
                    keyChoice.setSelectedItem("");
                }
            } else {
                keyChoice.setSelectedItem("");
            }
            nameTextField.repaint();
        }
    }

    @Override
    public boolean validateForm() {
        boolean valid = true;
        StringBuilder builder = new StringBuilder();
        if (ValidationUtils.isEmpty(nameTextField.getText())) {
            builder.append("Name must not be empty.\n");
            valid =
                    false;
        }
        if (!valid) {
            JOptionPane.showMessageDialog(null,
                    builder.toString(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return valid;
    }
}
